//设置Layer
//用法：构造(然后显示)
//全局变量：音量

var setting = {
	musicVolume : 5,
	
	effectVolume : 5,
	
	RefreshVolume : function(){
		if (this.musicVolume > 5)
			this.musicVolume = 5;
		if (this.musicVolume < 0)
			this.musicVolume = 0;
		
		if (this.effectVolume > 5)
			this.effectVolume = 5;
		if (this.effectVolume < 0)
			this.effectVolume = 0;
		
		cc.audioEngine.setMusicVolume(this.musicVolume * 0.2);
		cc.audioEngine.setEffectsVolume(this.effectVolume * 0.2);
	}
};

var SettingLayer = CanvasLayer.extend({
	
	musicVolumeLabel : null,
	
	effectVolumeLabel : null,

	ini : function() {
		var winSize = cc.director.getWinSize();
		
		var musicLabel = cc.LabelTTF.create("音乐音量：", "simhei", 48);
		var musicUp = this._CreateVerticalButton("↑", 0, 0, this._OnMusicVolumeUp);
		this.musicVolumeLabel = cc.LabelTTF.create(FormatNumber(setting.musicVolume, 3), "simhei", 48);
		var musicDown = this._CreateVerticalButton("↓", 0, 0, this._OnMusicVolumeDown);
		
		var effectLabel = cc.LabelTTF.create("音效音量：", "simhei", 48);
		var effectUp = this._CreateVerticalButton("↑", 0, 0, this._OnEffectVolumeUp);
		this.effectVolumeLabel = cc.LabelTTF.create(FormatNumber(setting.effectVolume, 3), "simhei", 48);
		var effectDown = this._CreateVerticalButton("↓", 0, 0, this._OnEffectVolumeDown);
		
		ArrangeVertical(0.5 * winSize.width, 0.45 * winSize.height, 0.12 * winSize.height, [musicLabel, effectLabel]);
		ArrangeHorizontal(0.5 * winSize.width, musicLabel.y, 0.03 * winSize.width, [musicLabel, musicUp, this.musicVolumeLabel, musicDown]);
		ArrangeHorizontal(0.5 * winSize.width, effectLabel.y, 0.03 * winSize.width, [effectLabel, effectUp, this.effectVolumeLabel, effectDown]);
		
		var menu = new cc.Menu(musicUp, musicDown, effectUp, effectDown);
		menu.attr({x : 0, y : 0});
		
		this.node.addChild(musicLabel);
		this.node.addChild(this.musicVolumeLabel);
		this.node.addChild(effectLabel);
		this.node.addChild(this.effectVolumeLabel);
		this.node.addChild(menu);
	},
	
	_CreateVerticalButton : function(text,xpos,ypos,callback){
		var button = new cc.MenuItemImage(resource.ctrlbtnNormalV_png,resource.ctrlbtnDownV_png, resource.ctrlbtnDisableV_png, callback, this);
		button.setPosition(xpos, ypos);
		var label = cc.LabelTTF.create(text, "simhei", 28);
		label.setPosition(button.width * 0.5, button.height * 0.5);
		label.setColor(cc.color(0,0,0));
		button.addChild(label);
		return button;
	},
	
	_OnMusicVolumeUp : function(sender){
		setting.musicVolume++;
		setting.RefreshVolume();
		this.musicVolumeLabel.setString(FormatNumber(setting.musicVolume, 3));
	},
	
	_OnMusicVolumeDown : function(sender){
		setting.musicVolume--;
		setting.RefreshVolume();
		this.musicVolumeLabel.setString(FormatNumber(setting.musicVolume, 3));
	},
	
	_OnEffectVolumeUp : function(sender){
		setting.effectVolume++;
		setting.RefreshVolume();
		this.effectVolumeLabel.setString(FormatNumber(setting.effectVolume, 3));
	},
	
	_OnEffectVolumeDown : function(sender){
		setting.effectVolume--;
		setting.RefreshVolume();
		this.effectVolumeLabel.setString(FormatNumber(setting.effectVolume, 3));
	}
});